package com.nd.teamfile.fileexplorer.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nd.teamfile.fileexplorer.R;
import com.nd.teamfile.fileexplorer.helper.FileIconHelper;
import com.nd.teamfile.fileexplorer.sdk.type.FileExplorerInfo;
import com.nd.teamfile.fileexplorer.util.FileExplorerUtil;

/**
 * 文件列表适配器
 * 
 * @ClassName FileListAdapter
 * @Function TODO ADD FUNCTION
 * @Reason TODO ADD REASON
 * 
 * @author HuangYK
 * @version v1.3.3
 * @Date 2014-1-27 下午4:30:56
 * 
 * @see
 */
public class FileListAdapter extends ArrayAdapter<FileExplorerInfo> {
	private LayoutInflater mInflater;
	private FileIconHelper mFileIcon;
	private Context mContext;

	public FileListAdapter(Context context, List<FileExplorerInfo> objects,
			FileIconHelper fileIcon) {
		super(context, R.layout.file_explorer_item, objects);
		mInflater = LayoutInflater.from(context);
		mFileIcon = fileIcon;
		mContext = context;
	}

	@Override
	public int getCount() {
		return super.getCount();
	}

	@Override
	public long getItemId(int position) {
		return super.getItemId(position);
	}

	@Override
	public FileExplorerInfo getItem(int position) {
		return super.getItem(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.file_explorer_item, null);
			// view = mInflater
			// .inflate(R.layout.file_explorer_item, parent, false);
			convertView.setTag(createViewHolder(convertView));
		}
		FileExplorerInfo lFileInfo = getItem(position);
		ViewHolder viewHolder = (ViewHolder) convertView.getTag();
		setupFileListItemInfo(viewHolder, lFileInfo);
		return convertView;
	}

	private ViewHolder createViewHolder(View convertView) {
		ViewHolder viewHolder = new ViewHolder();
		viewHolder.ivSelect = (ImageView) convertView
				.findViewById(R.id.ivSelect);
		viewHolder.ivFileIcon = (ImageView) convertView
				.findViewById(R.id.ivFileIcon);
		// viewHolder.ivFileIconFrame = (ImageView) convertView
		// .findViewById(R.id.ivFileIconFrame);
		viewHolder.tvFileName = (TextView) convertView
				.findViewById(R.id.tvFileName);
		viewHolder.tvFileCount = (TextView) convertView
				.findViewById(R.id.tvFileCount);
		viewHolder.tvFileSize = (TextView) convertView
				.findViewById(R.id.tvFileSize);
		viewHolder.tvModifiedTime = (TextView) convertView
				.findViewById(R.id.tvModifiedTime);
		return viewHolder;
	}

	private class ViewHolder {
		ImageView ivSelect;// 右侧图标
		ImageView ivFileIcon;// 文件缩略图
		// ImageView ivFileIconFrame;// 文件缩略图边框

		TextView tvFileName;// 文件名
		TextView tvFileCount;// 文件名
		TextView tvFileSize;// 文件大小
		TextView tvModifiedTime;// 修改时间
	}

	/**
	 * 设置文件列表Item的状态
	 * 
	 * @n<b>函数名称</b> :setupFileListItemInfo
	 * @brief
	 * @see
	 * @version Ver 1.3.3
	 * @param @param context
	 * @param @param view
	 * @param @param fileInfo
	 * @param @param fileIcon
	 * @param @param fileViewInteractionHub
	 * @return void
	 * @<b>作者</b> : HuangYK
	 * @<b>创建时间</b> : 2014-1-25下午12:08:11
	 */
	private void setupFileListItemInfo(ViewHolder viewHolder,
			FileExplorerInfo fileInfo) {
		// 选择框
		viewHolder.ivSelect.setVisibility(View.VISIBLE);
		// 如果是文件，则显示箭头
		if (fileInfo.IsDir) {
			viewHolder.ivSelect.setImageResource(R.drawable.arrow_folder);
		} else {
			// 根据选中状态设置图标
			viewHolder.ivSelect
					.setImageResource(fileInfo.Selected ? R.drawable.btn_check_on_holo_light
							: R.drawable.btn_check_off_holo_light);
			viewHolder.ivSelect.setTag(fileInfo);
			// 是否选中
			// view.setSelected(fileInfo.Selected);
		}
		// 设置文件名
		viewHolder.tvFileName.setText(fileInfo.fileName);
		// 如果是文件夹，则设置文件个数
		viewHolder.tvFileCount.setText(fileInfo.IsDir ? "(" + fileInfo.Count
				+ ")" : "");
		// 设置修改时间
		viewHolder.tvModifiedTime.setText(FileExplorerUtil.formatDateString(
				mContext, fileInfo.ModifiedDate));
		// 如果是文件夹，则不显示文件大小
		viewHolder.tvFileSize.setText((fileInfo.IsDir ? "" : FileExplorerUtil
				.convertStorage(fileInfo.fileSize)));
		// 如果是文件夹，则隐藏边框。否则去获取图标
		if (fileInfo.IsDir) {
			// viewHolder.ivFileIconFrame.setVisibility(View.GONE);
			if (fileInfo.Count > 0)
				viewHolder.ivFileIcon.setImageResource(R.drawable.folder);
			else
				viewHolder.ivFileIcon.setImageResource(R.drawable.folder_empty);
		} else {
			mFileIcon.setIcon(fileInfo, viewHolder.ivFileIcon);
		}
	}
}
